package huxibianjie.com.lbscandroid.ui.new_activity;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.event.LoginEvent;
import huxibianjie.com.lbscandroid.manager.RouteManager;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.util.StringUtils;
import huxibianjie.com.lbscandroid.widget.CountdownTextView;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.et_phone_number)
    EditText mPhoneNumber;
    @BindView(R.id.del_phone_number)
    ImageView mDelPhoneNumber;
    @BindView(R.id.et_phone_code)
    EditText mPhoneCode;
    @BindView(R.id.get_code)
    CountdownTextView countdownTextView;
    @BindView(R.id.btn_login)
    Button mBtnLogo;
    @BindView(R.id.et_user_password)
    EditText mUserPassword;
    @BindView(R.id.del_user_password)
    ImageView mDelUserPassword;
    @BindView(R.id.user_password_login)
    RelativeLayout mUserPasswordLogin;
    @BindView(R.id.get_code_login)
    RelativeLayout mGetCodeLogin;
    @BindView(R.id.switch_type)
    TextView switchType;
    private int type;//0 验证码登录  1 密码登录

    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        initTitle(R.drawable.ic_back, "登录");
        type = 1;
        switchType();
    }

    @Override
    public void onLoginEvent(LoginEvent event) {
        super.onLoginEvent(event);
        if (event.isLogin) {
            finish();
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initListener() {
        mPhoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    mDelPhoneNumber.setVisibility(View.VISIBLE);
                } else {
                    mDelPhoneNumber.setVisibility(View.INVISIBLE);
                }
            }
        });
        mUserPassword.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    mDelUserPassword.setVisibility(View.VISIBLE);
                } else {
                    mDelUserPassword.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    private void switchType() {
        if (type == 1) {
            switchType.setText("验证码登录");
            mUserPasswordLogin.setVisibility(View.VISIBLE);
            mGetCodeLogin.setVisibility(View.GONE);
        } else {
            switchType.setText("密码登录");
            mUserPasswordLogin.setVisibility(View.GONE);
            mGetCodeLogin.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.del_phone_number, R.id.del_user_password, R.id.get_code,
            R.id.tv_register, R.id.switch_type,
            R.id.btn_login, R.id.tv_forget_password,
    })
    public void onClick(View v) {
        final String phone = mPhoneNumber.getText().toString();
        switch (v.getId()) {
            case R.id.switch_type:
                type = type == 1 ? 0 : 1;
                switchType();
                break;
            case R.id.tv_register:
                RouteManager.getInstance().showRegisterActivity(this);
                break;
            case R.id.tv_forget_password:
                RouteManager.getInstance().showForgetPasswordActivity(this);
                break;
            case R.id.del_phone_number:
                mPhoneNumber.setText("");
                break;
            case R.id.del_user_password:
                mUserPassword.setText("");
                break;
            case R.id.get_code:
                if (!StringUtils.isMobile(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                showLoading();
                Task.create(Service.class).sentCode(phone).enqueue(new huxibianjie.com.lbscandroid.task.Callback<Map<String, String>>() {
                    @Override
                    public void onSuccess(@Nullable Map<String, String> map) {
                        cancelLoading();
                        showToast("验证码已发送,请注意查收");
                        countdownTextView.start();
                    }

                    @Override
                    public void onFailure(@NonNull String error) {
                        showToast(error);
                        cancelLoading();
                    }
                });
                break;
            case R.id.btn_login:
                if (!StringUtils.isMobile(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                //mIsLogin 验证码登录  else 密码登录;
                if (type == 0) {
                    String verification_code = mPhoneCode.getText().toString();
                    if (StringUtils.isEmpty(verification_code)) {
                        showToast("请输入正确的验证码");
                        mPhoneCode.requestFocus();
                        mPhoneCode.setSelection(mPhoneCode.getText().toString().length());
                        return;
                    }
                    showLoading();
                    Task.create(Service.class).loginByCode(phone, verification_code).enqueue(new huxibianjie.com.lbscandroid.task.Callback<UserModel>() {
                        @Override
                        public void onSuccess(@Nullable UserModel userModel) {
                            cancelLoading();
                            if (userModel != null) {
                                UserManager.getInstance().save(userModel);
                                if (!userModel.isHaveFlag()) {
                                    RouteManager.getInstance().showCompleteCipherActivity(LoginActivity.this, phone);
                                }
                                finish();
                            } else {
                                showToast("登录失败");
                            }
                        }

                        @Override
                        public void onFailure(@NonNull String error) {
                            cancelLoading();
                            showToast(error);
                        }
                    });
                } else {
                    String password = mUserPassword.getText().toString();
                    if (StringUtils.isEmpty(password)) {
                        showToast("请输入账号密码");
                        mUserPassword.requestFocus();
                        mUserPassword.setSelection(mUserPassword.getText().toString().length());
                        return;
                    }
                    showLoading();
                    Task.create(Service.class).loginByPass(phone, password).enqueue(new Callback<UserModel>() {
                        @Override
                        public void onSuccess(@Nullable UserModel userModel) {
                            cancelLoading();
                            if (userModel != null) {
                                UserManager.getInstance().save(userModel);
                                finish();
                            } else {
                                showToast("登录失败");
                            }
                        }

                        @Override
                        public void onFailure(@NonNull String error) {
                            cancelLoading();
                            showToast(error);
                        }
                    });
                }
                break;

        }
    }
}
